const connection = require('../app/database');

class PictrueService {

  // 保存一张图片
  async create(userId, mimetype, height, width, url) {
    try {
      const statement = `INSERT INTO picture (user_id, mimetype, height, width, url) VALUES (?, ?, ?, ?, ?)`;
      const result = await connection.execute(statement, [userId, mimetype, height, width, url]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 删除一张图片
  async remove(userId, pictureId) {
    try {
      const statement = `DELETE FROM picture WHERE user_id = ? AND id = ?`;
      const result = await connection.execute(statement, [userId, pictureId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 获取图片列表
  async list(userId) {
    try {
      const statemtnt = `SELECT id, user_id userId, mimetype, height, width, url FROM picture WHERE user_id = ?`;
      const result = await connection.execute(statemtnt, [userId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

}

module.exports = new PictrueService();

